package top.xiaoshuaila.searescue.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.xiaoshuaila.searescue.controller.index;
import top.xiaoshuaila.searescue.pojo.Helmet;
import top.xiaoshuaila.searescue.pojo.Serial;
import top.xiaoshuaila.searescue.pojo.Tag;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Component
public class MqttDataDeal2 {
    /*
     * 0:当前电压(mv)
     * 1:
     * 2:当前气压(Pa)
     * 3:
     * 4:当前气温(0.01℃)
     * 5:
     * 6:测距结果1:距离(cm)
     * 7:
     * 8:测距结果1:对方编号
     * 9:测距结果1:保持时间(s)
     * 10:测距结果2:距离(cm)
     * 11:
     * 12:测距结果2:对方编号
     * 13:测距结果2:保持时间(s)
     * 14:测距结果3:距离(cm)
     * 15:
     * 16:测距结果3:对方编号
     * 17:测距结果3:保持时间(s)
     * 18:测距结果4:距离(cm)
     * 19:
     * 20:测距结果4:对方编号
     * 21:测距结果4:保持时间(s)
     * 22:测距结果5:距离(cm)
     * 23:
     * 24:测距结果5:对方编号
     * 25:测距结果5:保持时间(s)
     * 26:头盔状态
     * 27:事件寄存器
     * 28:控制寄存器
     * 29:设备id
     * */
     int[] arr=new int[30];
     int[] wei=new int[]{
            4,0,4,0,4,0,4,0,2,2,
            4,0,2,2,4,0,2,2,4,0,
            2,2,4,0,2,2,2,2,2,2};
     @Autowired
     private index index;
     @Autowired
     private Tag tag;
     @Autowired
     private Serial serial;
     @Autowired
     private MqttService mqttService;
     public Map<Integer, Helmet> map=new HashMap<>();
     int cha=0;
     public Map<Integer,Integer>lostTime=new HashMap<>();
    public void detail(int[]tem){
        System.out.println(Arrays.toString(tem));
        try{
            if(tem[0]!=1||tem[1]!=4)return;
            int sta=3;
            for(int i=0;i<tem[2]/2;i++)
            {
                if(wei[i]==2)
                {
                    arr[i]=tem[sta]*(1<<8)+tem[sta+1];
                    sta+=2;
                }
                else if(wei[i]==4)
                {
                    arr[i]=tem[sta]*(1<<8)+tem[sta+1]+tem[sta+2]*(1<<24)+tem[sta+3]*(1<<16);
                    sta+=4;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        Helmet helmet=new Helmet();
        helmet.setId(arr[29]&255);
        if(helmet.getId()!=7)
            helmet.setId(helmet.getId()+10);
        helmet.setQiyayuzhi(mqttService.yuzhi);
        helmet.setStatue(arr[26]&3);
        helmet.setButton((arr[27]&1)!=0);
        helmet.setDianya(Float.intBitsToFloat(arr[0]));
        helmet.setQiya(Float.intBitsToFloat(arr[2]));
        helmet.setQiwen(Float.intBitsToFloat(arr[4]));
        for(int i=0;i<5;i++)
        {
            int idx=6+4*i;
            helmet.getDistances()[i]=new Object[]{Float.intBitsToFloat(arr[idx]),arr[idx+2],arr[idx+3]};
        }
        System.out.println(helmet);
        if(helmet.getId()==mqttService.arrid[0])
        {
            mqttService.yuzhi=helmet.getQiya();
            System.out.println("生成的阈值:"+mqttService.yuzhi);
        }
        else {
            Float qiya=helmet.getQiya();
            Float yuzhi= mqttService.yuzhi;
            if(serial.getSystemStatus()==2);
            else if(serial.getQiangzhi().contains(helmet.getId()));
            else if(helmet.isButton()||qiya>yuzhi+cha)
            {
                tag.setTAG(helmet.getId());
                if(serial.getSystemStatus()==0)
                {
                    serial.setSystemStatus(1);
                    if(helmet.getStatue()==0)
                        mqttService.Taskmsg=2;
                }
                else if(serial.getSystemStatus()==1)
                {
                    if(helmet.getStatue()==0)
                        mqttService.Taskmsg=2;
                }
                else if(serial.getSystemStatus()==2)
                    if(helmet.getStatue()!=2)
                        mqttService.Taskmsg=3;
            }
            else {
                if(Objects.equals(tag.getTAG(), helmet.getId()))
                {
                    serial.setSystemStatus(0);
                    if(helmet.getStatue()==1)
                        mqttService.Taskmsg=1;
                }
            }
        }
        map.put(helmet.getId(),helmet);
        if(helmet.getId()!=mqttService.arrid[0])
        {
            lostTime.put(helmet.getId(),0);
        }
    }
}